package com.world.rickcloudendpoint.api.controller;

import com.world.rickcloudendpoint.common.Result;
import com.world.rickcloudendpoint.common.constant.DetailErrorCodes;
import com.world.rickcloudendpoint.common.constant.ErrorCodes;
import com.world.rickcloudendpoint.common.exception.ClientException;
import com.world.rickcloudendpoint.common.exception.ModuleException;
import com.world.rickcloudendpoint.logic.service.ISemaphoreService;
import com.world.rickcloudendpoint.models.entity.Semaphore;
import com.world.rickcloudendpoint.service.uuid.LongIdGenerator;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @description: 音箱外部访问接口
 * @author: jlsong
 * @date: 2022/01/21 23:02:36
 * @version: 1.0
 */
@Log4j2
@RestController
public class SemaphoreController implements SemaphoreApi {

    final ISemaphoreService semaphoreService;

    public SemaphoreController(ISemaphoreService semaphoreService) {
        this.semaphoreService = semaphoreService;
    }

    @Override
    public Result<Semaphore> addSemaphore(Semaphore payload) throws ClientException, ModuleException {
        // TODO 校验 参数
        if (payload == null) {
            throw new ClientException(ErrorCodes.getApplicationUi(DetailErrorCodes.Request_Param_Null.value()), DetailErrorCodes.Request_Param_Null.getReasonPhrase());
        }
        // TODO 校验参数体的一些属性值是否唯一
        Long id = LongIdGenerator.generateId();
        payload.setId(id + "");
        semaphoreService.save(payload);
        Semaphore ret = semaphoreService.getById(id + "");
        return Result.ok(ret);
    }

    @Override
    public Result<Semaphore> editSemaphore(Semaphore payload) throws ClientException, ModuleException {
        return null;
    }

    @Override
    public Result<Long> delSemaphores(String ids) throws ClientException, ModuleException {
        return null;
    }

    @Override
    public Result<Object> addSemaphores(List<Semaphore> payload) throws ClientException, ModuleException {
        return null;
    }

    @Override
    public Result<Object> listSemaphores(@ApiParam(value = "查询过滤表达式，例如:((a|b)&c)|((a&b&d)|e)") @RequestParam(value = "exp", required = false) String exp,
                                         @ApiParam("查询过滤表达式对应字典sql，例如：{'a': object, 'b': object, 'c': object, 'd': object, 'e': object}。说明：object->{'field': 'id', 'op': 'eq|ne|gt|lt|gte|lte|in|nin|like', 'value': 1}") @RequestParam(value = "dsl", required = false) String dsl,
                                         @ApiParam(value = "结果列表偏移量", defaultValue = "0") @RequestParam(value = "offset", required = false, defaultValue = "0") Long offset,
                                         @Min(-1) @ApiParam(value = "预期获取数量,最小值-1表示获取全部", defaultValue = "200") @RequestParam(value = "size", required = false, defaultValue = "200") Integer size,
                                         @NotNull @ApiParam(value = "排序字段，默认为添加时间倒序", required = true) @RequestParam(value = "sortField") String sortField,
                                         @NotNull @ApiParam(value = "排序方式:asc|desc，默认倒序desc", required = true, allowableValues = "desc,asc") @RequestParam(value = "sortBy") String sortBy) throws ClientException, ModuleException {
        return null;
    }
}
